package cn.bqjr.eily.shiro.spring.boot;

import cn.bqjr.eily.shiro.spring.boot.entity.TenantUserPwdToken;
import cn.bqjr.eily.shiro.spring.boot.utils.EncryptUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.springframework.stereotype.Component;

/**
 * 用户登录密码校验器
 */
@Component("userPwdCredentialsMatcher")
public class UserPwdCredentialsMatcher extends SimpleCredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        TenantUserPwdToken utoken = (TenantUserPwdToken) token;
        String inPassword = new String(utoken.getPassword());
        String dbPassword = (String) info.getCredentials();
        try {
            return this.equals(EncryptUtils.encryptPwd(inPassword), dbPassword);
        } catch (Exception e) {
            return false;
        }
    }
}
